package com.ptp.backendcertms.networksecurity.filter.config;

import com.ptp.backendcertms.networksecurity.filter.exception.ExceptionFilter;
import com.ptp.backendcertms.networksecurity.filter.form.FormRequestFilter;
import com.ptp.backendcertms.networksecurity.filter.sqlxss.CrossSqlXssFilter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import static com.ptp.backendcertms.api.internal.constant.CertMsConstant.*;

/**
 * 过滤器配置：跨域、sql注入、xss注入过滤器；B端token过滤器；B端表单过滤器；C端token过滤器；C端表单过滤器
 *
 * @author songcongcong
 * @date 2022/3/9
 */
@Configuration
public class FilterConfig {

    private final String context = "/cms";
    /**
     * formRequestFilter中需要经过过滤器的接口
     */
    private final String[] formRequestFilterIncludeUrls = new String[]{
            context + "/file/importCertInfo"
    };

    /**
     * 异常过滤器
     * 在捕获到过滤器级别异常时可以打印出异常信息
     */
    @Bean
    public FilterRegistrationBean<ExceptionFilter> exceptionFilterBean() {
        FilterRegistrationBean<ExceptionFilter> filterRegistrationBean = new FilterRegistrationBean<>();
        filterRegistrationBean.setFilter(exceptionFilter());
        filterRegistrationBean.addUrlPatterns(FUZZY_MATCHING_SYMBOL);
        filterRegistrationBean.setName("exceptionFilter");
        filterRegistrationBean.setOrder(0);
        return filterRegistrationBean;
    }

    /**
     * 跨域、防sql注入、防xss注入过滤器（安全套件）
     */
    @Bean
    public FilterRegistrationBean<CrossSqlXssFilter> crossSqlXssFilterRegistrationBean() {
        FilterRegistrationBean<CrossSqlXssFilter> filterRegistrationBean = new FilterRegistrationBean<>();
        filterRegistrationBean.setFilter(crossSqlXssFilter());
        filterRegistrationBean.addUrlPatterns(FUZZY_MATCHING_SYMBOL);
        filterRegistrationBean.setName("crossSqlXssFilter");
        filterRegistrationBean.setOrder(1);
        return filterRegistrationBean;
    }

    /**
     * B端表单过滤器
     * 用于B端的表单提交、用户识别和固定参数加载
     */
    @Bean
    public FilterRegistrationBean<FormRequestFilter> formRequestFilterRegistrationBean() {
        FilterRegistrationBean<FormRequestFilter> filterRegistrationBean = new FilterRegistrationBean<>();
        filterRegistrationBean.setFilter(formRequestFilter());
        filterRegistrationBean.addUrlPatterns(FUZZY_MATCHING_SYMBOL);
        filterRegistrationBean.setName("formRequestFilter");
        filterRegistrationBean.setOrder(3);
        filterRegistrationBean.addInitParameter(INCLUDED_URLS, StringUtils.join(formRequestFilterIncludeUrls, ENGLISH_COMMA_SEPARATOR));
        return filterRegistrationBean;
    }

    @Bean
    public ExceptionFilter exceptionFilter() {
        return new ExceptionFilter();
    }

    @Bean
    public CrossSqlXssFilter crossSqlXssFilter() {
        return new CrossSqlXssFilter();
    }

    @Bean
    public FormRequestFilter formRequestFilter() {
        return new FormRequestFilter();
    }
}